﻿using System;
using System.Collections.Generic; 
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.JCI.Common.Entity;
using Microsoft.JCI.Bll;
using System.Text;

namespace Microsoft.JCI.Portal.Agent
{
    public partial class DispatchOrderListByCustomer : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
             
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            GetCustomerList(AspNetPager1.CurrentPageIndex);
        }

        private void GetCustomerList(int PageIndex)
        {
            MasterDataBll bll = new MasterDataBll();
            StringBuilder sb = new StringBuilder();
            if (txtName.Text.Trim() != string.Empty)
            {
                sb.Append(" and Name like '%");
                sb.Append(txtName.Text.Trim());
                sb.Append("%'");
            }
            if (txtMobile.Text.Trim() != string.Empty)
            {
                sb.Append(" and Mobile like '%");
                sb.Append(txtName.Text.Trim());
                sb.Append("%'");
            }

            int RecordCount = 0;
            List<MadCustomerEntity> list = bll.GetAll<MadCustomerEntity>(sb.ToString(), "order by CreatedOn Desc", AspNetPager1.PageSize, PageIndex, out RecordCount);

            radGridCustomerList.DataSource = list;
            radGridCustomerList.DataBind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            GetCustomerList(AspNetPager1.CurrentPageIndex);
        }
        
        protected void radGridCustomerList_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e)
        {
            MasterDataBll bll = new MasterDataBll();
            StringBuilder sb = new StringBuilder();
            if (txtName.Text.Trim() != string.Empty)
            {
                sb.Append(" and Name like '%");
                sb.Append(txtName.Text.Trim());
                sb.Append("%'");
            }
            if (txtMobile.Text.Trim() != string.Empty)
            {
                sb.Append(" and Mobile like '%");
                sb.Append(txtName.Text.Trim());
                sb.Append("%'");
            }

            int RecordCount = 0;
            List<MadCustomerEntity> list = bll.GetAll<MadCustomerEntity>(sb.ToString(), "order by CreatedOn Desc", AspNetPager1.PageSize, 1, out RecordCount);

            radGridCustomerList.DataSource = list;
        }

        protected void radGridCustomerList_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            {
                MadCustomerEntity entity = e.Item.DataItem as MadCustomerEntity;
                LinkButton lnkbtnAddNew = e.Item.FindControl("lnkbtnAddNew") as LinkButton;
                lnkbtnAddNew.Attributes.Add("onclick", "openMyFullWin('../Dispatch/AgentDispatchOrderAdd.aspx?CustomerID=" + entity.CustomerId + "','AgentDispatchOrderAdd');");

                SystemBll bll = new SystemBll();
                Label lblProvince = e.Item.FindControl("lblProvince") as Label;
                Label lblCity = e.Item.FindControl("lblCity") as Label;
                SysDictionaryEntity dict = bll.GetDictionaryFromCache(entity.Province);
                if (dict != null)
                {
                    lblProvince.Text = dict.ValueCN;
                }

                dict = bll.GetDictionaryFromCache(entity.City);
                if (dict != null)
                {
                    lblCity.Text = dict.ValueCN;
                }
            }
        }
    }
}
